﻿using System;
using System.Web;
using System.Threading;
using System.Text;
using System.Net;
using System.Collections.Generic;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using blackhouse.BlackApart.Web;
using mojoPortal.Web.Framework;
using blackhouse.BlackApart.Web.Handlers;

namespace blackhouse.Aparts.Web {
    public class UpdateModule : IHttpModule {

        #region IHttpModule Members

        private static UpdateKeepAlive uka;
        //private static Thread updateThread;
        //private static Dictionary<int, Thread> updateThreads = new Dictionary<int, Thread>();
        //private static object lockObj = new object();

        public void Dispose() {
            //clean-up code here.
        }

        public void Init(HttpApplication context) {
            context.BeginRequest += new EventHandler(BeginRequest);
        }

        void BeginRequest(object sender, EventArgs e) {
            if (ConfigHelper.GetBoolProperty("DisableUpdate", false)) return;
            if (uka == null) uka = new UpdateKeepAlive();
            //return;
            //int siteCount = mojoPortal.Business.SiteSettings.SiteCount();
            //lock (lockObj) {
            //    for (int xt = 1; xt <= siteCount; xt++) {


            //        if (updateThreads.ContainsKey(xt)
            //                && (updateThreads[xt].ThreadState == ThreadState.Aborted
            //                || updateThreads[xt].ThreadState == ThreadState.Stopped)) {
            //            updateThreads[xt].Abort();
            //            updateThreads.Remove(xt);
            //        }
            //        if (!updateThreads.ContainsKey(xt)) {
            //            Thread updateThread = this.StartThreadForSite(xt);
            //            if (updateThread != null)
            //                updateThreads[xt] = updateThread;
            //        }
            //    }
            //}
        }

        //private Thread StartThreadForSite(int siteId) {
        //    SiteSettings site = CacheHelper.GetSiteSettings(siteId);
        //    if (site == null) return null;
        //    string folder = "";

        //    List<SiteFolder> folders = SiteFolder.GetBySite(site.SiteGuid);
        //    if (folders.Count > 0)
        //        folder = folders[0].FolderName;
        //    string url = UrlUtils.LocalizeUrl(folder, "/Update.aspx");
        //    url = this.GetPathFromRoot(url);
        //    Thread updateThread = new Thread(new ParameterizedThreadStart(StartThreadUpdate));
        //    updateThread.Name = "UpdateThread-" + siteId;
        //    updateThread.IsBackground = true;
        //    updateThread.Start(url);
        //    return updateThread;
        //}

        //private void StartThreadUpdate(object url) {
        //    while (true) {
        //        try {
        //            WebRequest request = WebRequest.Create(url.ToString());
        //            WebResponse response = request.GetResponse();
        //            response.Close();
        //        }
        //        catch (Exception) {
        //            continue;
        //        }
        //        Thread.Sleep(TimeSpan.FromMinutes(1));
        //    }
        //}

        //private string GetPathFromRoot(string pathWithoutRoot) {
        //    return "http://" + HttpContext.Current.Request.Url.Host + pathWithoutRoot;
        //}

        #endregion

    }
}
